package com.thingple.otg;

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.TextView;

import java.util.Locale;

public class UsbActivity extends AppCompatActivity {

    private static final String TAG = UsbActivity.class.getName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usb);
        init();
    }

    private void viewMsg(String msg) {
        new Handler(Looper.getMainLooper()).post(() -> {
            TextView v = findViewById(R.id.label_msg);
            v.setText(msg);
        });
    }

    private void init() {

        UsbManager manager = getSystemService(UsbManager.class);
        if (manager == null) {
            Log.d(TAG, "Usb manager null");
            return;
        }
        viewMsg("获取到Usb Manager");
        UsbAccessory[] list = manager.getAccessoryList();
        if (list == null) {
            Log.d(TAG, "Accessory null");
            viewMsg("Accessory 空");
            return;
        }
        for (UsbAccessory a : list) {
            Log.i(TAG, a.getManufacturer() + ":" + a.getModel());
            viewMsg(String.format("%s:%s[%s]", a.getManufacturer(), a.getModel(), a.getDescription()));
        }
    }
}